﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Surface.Core;
using Microsoft.Surface.Core.Manipulations;
using Microsoft.Xna.Framework.Input;

namespace Sublimation
{
    class MenuSurf : Menu
    {
        protected float prevOrientation;
        protected float prevX;
        protected float prevY;
        protected Contact contact;
        public Contact Contact
        {
            get { return contact; }
            set { contact = value; }
        }

        public MenuSurf(WorldProperties worldProperties,Contact contact)
            : base(worldProperties)
        {
            this.contact = contact;
        }

        public void AddItem(String aText)
        {
            if (menuItems.Count == 0)
            {
                MenuItemSurf newItem = new MenuItemSurf(worldProperties, aText,
                    new Vector2(Position.X + sideMargin, Position.Y + sideMargin),
                    new Vector2(0, 0));

                menuItems.Add(newItem);
            }
            else
            {
                MenuItemSurf tempItem = (MenuItemSurf)menuItems.ElementAt(menuItems.Count - 1);
                MenuItemSurf newItem = new MenuItemSurf(worldProperties, aText,
                    new Vector2(tempItem.Position.X + tempItem.Sprite.Width + itemMargin, tempItem.Position.Y),
                    new Vector2(0, 0));

                menuItems.Add(newItem);
            }
        }

        // Method for rotating and translating the huds
        public void Transform(Contact aContact)
        {
            // current hud position
            float curX = aContact.X;
            float curY = aContact.Y;

            float deltaX;
            float deltaY;

            if (prevX != 0.0f && prevY != 0.0f)
            {
                // translation deltas 
                deltaX = curX - prevX;
                deltaY = curY - prevY;
            }
            else
            {
                deltaX = 0.0f;
                deltaY = 0.0f;
            }

            // save x,y values
            prevX = curX;
            prevY = curY;

            // translate the hud to current point 
            this.Position = new Vector2(curX, curY);

            for(int i = 0 ; i < menuItems.Count; i++)
            {
                MenuItemSurf aItem = (MenuItemSurf) menuItems.ElementAt(i);
                    aItem.Position = new Vector2(aItem.Position.X + deltaX, aItem.Position.Y + deltaY);
            }

            float currentOrientation = aContact.Orientation;

            if (prevOrientation != 0.0f)
            {
                // rotation delta
                float deltaRotation = currentOrientation - prevOrientation;
                // rotate the hud container
                rotation += deltaRotation;
                AlignItems(deltaRotation);
            }

            // save current rotation
            prevOrientation = currentOrientation;

        }

        public void AlignItems(float deltaRotation)
        {
            // align each item related to rotation
            for (int i = 0; i < menuItems.Count; i++)
            {
                MenuItemSurf aItem = (MenuItemSurf)menuItems.ElementAt(i);
                aItem.Position = RotatePoint(this.Position, deltaRotation, aItem.Position);
                aItem.Rotation += deltaRotation;
            }
        }

        public Vector2 RotatePoint(Vector2 origin, float radians, Vector2 sourceVector)
        {
            Matrix myRotationMatrix = Matrix.CreateRotationZ(radians);

            // Rotate relative to origin.
            Vector2 rotatedVector = Vector2.Transform(sourceVector - origin, myRotationMatrix);

            // Add origin to get final location.
            sourceVector = rotatedVector + origin;
            return sourceVector;
        }

        // method for keeping position status of menu items inside the menu
        public void UpdateItemStatus(MenuItemSurf item)
        {
            Rectangle menuRectangle = new Rectangle((int)this.position.X, (int)this.position.Y, this.sprite.Width, this.sprite.Height);
            Rectangle itemRect = new Rectangle((int)item.Position.X, (int)item.Position.Y, item.Sprite.Width, item.Sprite.Height);
              
            if (itemRect != null)
            {
                if (menuRectangle.Intersects(itemRect))
                {
                    item.ItemStatus = MenuItemStatus.InMenu;
                    item.SpriteColor = Color.White;
                    item.TowerColor = Color.White;
                }
                else
                {
                    item.ItemStatus = MenuItemStatus.OutMenu;
                    // make it transparent
                    item.SpriteColor = new Color(255, 255, 255, 0.3f);
                    item.TowerColor = new Color(255, 255, 255, 0.3f);
                }
            }
        }

        #region HEX / INT conversion methods
        public static int HexToInt(String hex)
        {
            return Convert.ToInt32(hex, 16);
        }

        public static String IntToHex(int hex)
        {
            return hex.ToString("X");
        }
        #endregion

    }
}
